﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;
using LEEAAttendanceControl.General;

namespace LEEAAttendanceControl.Views
{
    public partial class Shifts : Page
    {
        public Shifts()
        {
            InitializeComponent();

            ReloadShifts();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.AddEvent += AddShiftHandler;
            myApp.EditEvent += EditShiftHandler;
            myApp.DeleteEvent += DeleteShiftHandler;

            myApp.ChangeEmployeesEvent += ReloadShiftsHandler;
        }

        public void ReloadShiftsHandler(object sender, EventArgs e)
        {
            ReloadShifts();
        }

        private void ReloadShifts()
        {
            ShiftsDataSource.QueryParameters.Clear();

            Parameter comParameter = new Parameter();

            comParameter.ParameterName = "companyID";
            comParameter.Value = ((App)Application.Current).CompanyID.Value;

            ShiftsDataSource.QueryParameters.Add(comParameter);

            ShiftsDataSource.Load();
        }

        private void ShiftsDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        public void AddShiftHandler(object sender, EventArgs e)
        {
            ShiftEdit newShift = new ShiftEdit("New Shift");

            newShift.LoadData(No.Zero);
            newShift.Closed += EditClosed;

            newShift.Show();
        }

        public void EditShiftHandler(object sender, EventArgs e)
        {
            Shift selectedShift = shiftsGrid.SelectedItem as Shift;

            ShiftEdit editShift = new ShiftEdit("Edit Shift");

            editShift.LoadData(selectedShift == null ? No.Zero : selectedShift.ID);
            editShift.Closed += EditClosed;

            editShift.Show();
        }

        public void EditClosed(object sender, EventArgs e)
        {
            ShiftsDataSource.Load();
        }

        public void DeleteShiftHandler(object sender, EventArgs e)
        {
            if (shiftsGrid.SelectedItem != null)
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.DeleteMessage
                    , ApplicationStrings.Delete, "Ask");

                message.Closed += (s, ee) =>
                {
                    ShiftsDataSource.DataView.Remove(shiftsGrid.SelectedItem);

                    ShiftsDataSource.SubmittedChanges += (ss, eee) =>
                    {
                        if (eee.HasError)
                        {
                            eee.MarkErrorAsHandled();
                            MessageDisplay message2 = new MessageDisplay(ApplicationStrings.GeneralError
                                , ApplicationStrings.GeneralError, "Error");

                            message2.Show();
                        }
                    };

                    ShiftsDataSource.SubmitChanges();
                };

                message.Show();
            }
        }

        private void shiftsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in shiftsGrid.ItemsSource)
            {
                Grid preGrid = shiftsGrid.Columns[6].GetCellContent(item) as Grid;

                if (preGrid != null)
                {
                    Ellipse circle = preGrid.Children.FirstOrDefault() as Ellipse;

                    if (circle != null)
                    {
                        circle.Opacity = 0;
                    }
                }
            }

            if (shiftsGrid.SelectedItem != null)
            {
                Grid grid = shiftsGrid.Columns[6].GetCellContent(shiftsGrid.SelectedItem) as Grid;

                if (grid != null)
                {
                    Ellipse circle = grid.Children.FirstOrDefault() as Ellipse;

                    if (circle != null)
                    {
                        circle.Opacity = 1;
                    }
                }
            }
        }
    }
}
